﻿//-----------------------------------------------------------------------
// <copyright file="TelemetryObjectHelper.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <summary>Helper Class to create some telemetry objects which are consumed by MrsTelemetryServiceProvider</summary>
// <author>v-hotong</author>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Reputation.Services;
using Microsoft.Reputation.Tests.FrontEndCommon;

namespace Microsoft.Reputation.Tests.ServiceabilityTest.Helper
{
    /// <summary>
    /// Helper Class to create some telemetry objects which are consumed by MrsTelemetryServiceProvider
    /// </summary>
    public class TelemetryObjectHelper
    {
        /// <summary>
        /// Create a list containing one valid block overrule
        /// </summary>
        /// <returns>BlockOverrule array</returns>
        public static BlockOverrule[] CreateValidBlockOverrules()
        {
            BlockOverrule[] list = new BlockOverrule[1];
            list[0] = new BlockOverrule(
                        TelemetryHelper.GetAction("Valid"),
                        TelemetryHelper.GetCategoryId("Valid"),
                        TelemetryHelper.GetUrl("Null"),         // Since both HostString and UrlString cannot be filled in and one has to be Empty, setting the HostString to be Null by default.
                        TelemetryHelper.GetUrl("Valid"));       // Choosing the HostString to be Null by Default over UrlString, as HostString is expected to be used less often than UrlString.
            return list;
        }

        /// <summary>
        /// Create a list containing one valid category count
        /// </summary>
        /// <returns>CategoryCount array</returns>
        public static CategoryCount[] CreateValidCategoryCounts()
        {
            CategoryCount[] list = new CategoryCount[1];
            list[0] = new CategoryCount(
                        TelemetryHelper.GetCategoryId("Valid"),
                        TelemetryHelper.GetUint("Valid"));
            return list;
        }

        /// <summary>
        /// Create a list containing one valid override exception
        /// </summary>
        /// <returns>OverrideException array</returns>
        public static OverrideException[] CreateValidOverrideExceptions()
        {
            OverrideException[] list = new OverrideException[1];
            list[0] = new OverrideException(
                        new uint[] { TelemetryHelper.GetCategoryId("Valid") },
                        TelemetryHelper.GetUrl("Null"),        // Since both HostString and UrlString cannot be filled in and one has to be Empty, setting the HostString to be Null by default.
                        TelemetryHelper.GetUrl("Valid"));      // Choosing the HostString to be Null by Default over UrlString, as HostString is expected to be used less often than UrlString.
            return list;
        }

        /// <summary>
        /// Create a list containing one valid sample Url
        /// </summary>
        /// <returns>SampleUrl array</returns>
        public static SampleUrl[] CreatetSampleUrls()
        {
            SampleUrl[] list = new SampleUrl[1];
            list[0] = new SampleUrl(
                        TelemetryHelper.GetUint("Valid"),
                        TelemetryHelper.GetUrl("Null"),    // Since both HostString and UrlString cannot be filled in and one has to be Empty, setting the HostString to be Null by default.
                        TelemetryHelper.GetUint("Valid"),
                        TelemetryHelper.GetUrl("Valid"));  // Choosing the HostString to be Null by Default over UrlString, as HostString is expected to be used less often than UrlString.
            return list;
        }

        /// <summary>
        /// Create a list containing one valid error report
        /// </summary>
        /// <returns>UrlErrorReport array</returns>
        public static UrlErrorReport[] CreateValidErrorReports()
        {
            UrlErrorReport[] list = new UrlErrorReport[1];
            UrlErrorReport temp = new UrlErrorReport();
            temp.ClientIPAddress = TelemetryHelper.GetIPAddress("Valid");
            temp.Comments = TelemetryHelper.GetUrl("Valid"); // Should have a separate function for returning Strings.
            temp.DisputedCategoryID = TelemetryHelper.GetCategoryId("Valid");
            temp.IsSiteOwner = true;
            temp.ReportClientID = TelemetryHelper.GetGUID("Valid");
            temp.ReportCustomerID = TelemetryHelper.GetGUID("Valid");
            temp.ReportCustomerVersion = TelemetryHelper.GetUrl("Valid"); // Should have a separate function for returning Strings.
            temp.ReportDeploymentID = TelemetryHelper.GetGUID("Valid");
            temp.ReportFidelity = TelemetryHelper.GetUint("Valid", 0, 4); // X is valid Fidelity is 0 <= X <= 3.
            temp.ReportLocale = TelemetryHelper.GetUrl("Valid");
            temp.SiteOwnerEmail = TelemetryHelper.GetUrl("Valid"); // Should have a separate function for returning Strings.
            temp.SiteOwnerName = TelemetryHelper.GetUrl("Valid");   // Should have a separate function for returning Strings.
            temp.SiteOwnerPhone = TelemetryHelper.GetUrl("Valid"); // Should have a separate function for returning Strings.
            temp.SuggestedCategories = CreateValidSuggestedCategories();
            temp.TargetURL = TelemetryHelper.GetUrl("Valid");
            list[0] = temp;
            return list;
        }

        /// <summary>
        /// Create a list containing one valid suggested category
        /// </summary>
        /// <returns>SuggestedCategory array</returns>
        public static SuggestedCategory[] CreateValidSuggestedCategories()
        {
            SuggestedCategory[] list = new SuggestedCategory[1];
            list[0] = new SuggestedCategory(
                        TelemetryHelper.GetCategoryId("Valid"),
                        TelemetryHelper.GetUint("Valid", 1, 6)); // SuggestedCategory.id can have valid values from 1-5, hence giving the range to Random. Giving 6 as max as Random returns X where min <= x < max.
            return list;
        }

        /// <summary>
        /// Create a list containing one valid url count
        /// </summary>
        /// <returns>UrlCounts object</returns>
        public static UrlCounts CreateValidUrlCounts()
        {  
            uint known = TelemetryHelper.GetUint("Valid");
            uint unknown = TelemetryHelper.GetUint("Valid");
            uint total;

            try
            {
                total = checked((uint)(known + unknown));
            }
            catch (System.OverflowException)
            {
                //overflow first time, do nothing
            }

            known = known / 2;
            unknown = unknown / 2;

            try
            {
                total = checked((uint)(known + unknown));
            }
            catch (System.OverflowException ex)
            {                    
                throw ex;
            }

            UrlCounts list = new UrlCounts(
                                        known,
                                        total,
                                        unknown);
            return list;    
        }       
    }
}